package com.cxg.fairy.ec.main.index.ad;

import android.annotation.SuppressLint;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.cxg.fairy.delegates.LatteDelegate;
import com.cxg.fairy.ec.main.EcBottomDelegate;
import com.cxg.fairy.ec.main.cart.ShopCartDelegate;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.IFailure;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;
import com.cxg.fairy.util.MyADDividerItemDecoration;
import com.cxg.fairy.util.toasty.Toasty;
import com.diabin.latte.ec.R;
import com.diabin.latte.ec.R2;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by CFairy
 */

public class ADGoodsListDelegate extends LatteDelegate implements ISuccess {

    private ADGoodsListAdapter mAdapter = null;

    private static final String ARG_ADGOODS_ID = "ARG_ADGOODS_ID";


    @BindView(R2.id.m_main_fab)
    FloatingActionButton mShopCartButton = null;

    @BindView(R2.id.m_main_back)
    FloatingActionButton mSearchListBackButton = null;

    @OnClick(R2.id.m_main_back)
    void doActionButtonBack() {
        getSupportDelegate().pop();
    }

    @OnClick(R2.id.back_home)
    void doBackHome(){ getSupportDelegate().pop();}

    @OnClick(R2.id.m_main_fab)
    void toShopCartDelegate() {
        if (getParentDelegate() != null) {
            getParentDelegate().getSupportDelegate().startWithPop(new EcBottomDelegate(3));
        } else {
            getSupportDelegate().startWithPop(new EcBottomDelegate(3));
        }
    }

    @BindView(R2.id.rv_ad_list_content)
    RecyclerView mRecyclerView = null;

    @BindView(R2.id.tv_search_tag_name)
    AppCompatTextView tvADGoodsTitle = null;

    @Override
    public Object setLayout() {
        return R.layout.delegate_list_ad_goods_a;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        mShopCartButton.setAlpha(0.7f);
        mSearchListBackButton.setAlpha(0.6f);
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        RestClient.builder()
                .url("getTemplatePageNativeById")
                .loader(getContext())
                .params("templatePageId",getArguments().getInt(ARG_ADGOODS_ID))
                .success(this)
                .build()
                .post();
    }

    public static ADGoodsListDelegate create(int goodsId) {
        final Bundle args = new Bundle();
        args.putInt(ARG_ADGOODS_ID, goodsId);
        final ADGoodsListDelegate delegate = new ADGoodsListDelegate();
        delegate.setArguments(args);
        return delegate;
    }

    public static ADGoodsListDelegate create(String goodsId) {
        final Bundle args = new Bundle();
        args.putString(ARG_ADGOODS_ID, goodsId);
        final ADGoodsListDelegate delegate = new ADGoodsListDelegate();
        delegate.setArguments(args);
        return delegate;
    }

    @SuppressLint("NewApi")
    @Override
    public void onSuccess(String response) {
        final ArrayList<MultipleItemEntity> data = new ADGoodsListDataConverter().setJsonData(response).convert();
        final int VListItemCount = new ADGoodsListDataConverter().setJsonData(response).getVItemCount();
        if (data.size() > 0) {
            MultipleItemEntity entity = data.get(0);
            String header = entity.getField(ADGoodsListItemFields.HEADER);
            tvADGoodsTitle.setText(header);
        }
        mAdapter = new ADGoodsListAdapter(data);
        final GridLayoutManager manager = new GridLayoutManager(getContext(), 30);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(mAdapter);
        MyADDividerItemDecoration itemDecoration = new MyADDividerItemDecoration(this.getContext(),VListItemCount);
        mRecyclerView.addItemDecoration(itemDecoration);
    }

}
